﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using iShoppingBUS;
using iShoppingDTO;

namespace iShopping.Member
{
    public partial class ChiTietGioHang : System.Web.UI.Page
    {
        private int tongTienThanhToan = 0;
        protected void Page_PreInit(object sender, EventArgs e)
        {
            GiaoDienDTO giaoDienDto = GiaoDienBUS.SelectGiaoDienMa("Theme001");
            if (giaoDienDto != null)
            {
                this.Theme = giaoDienDto.TenTheme;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if(this.Session.Count>0)
            {
                string mataikhoan = this.Session["userid"].ToString().Trim();

                if (!IsPostBack)
                {
                    rdBuuDien.Checked = true;
                    rdVanChuyen.Checked = false;
                    lblGiaoHang.Text = "50,000";

                    rdTienMat.Checked = true;
                    rdTanNoi.Checked = false;
                    rdTrucTuyen.Checked = false;
                    rdChuyenKhoan.Checked = false;
                    // end cong thanh toan

                    TaiKhoanBUS taiKhoanBus = new TaiKhoanBUS();
                    ThongTinTaiKhoanBUS thongTinTaiKhoanBus = new ThongTinTaiKhoanBUS();

                    DataTable dt = taiKhoanBus.LoaiTaiKhoan(mataikhoan);
                    string images = dt.Rows[0]["Avatar"].ToString().Trim();
                    if (images == "")
                    {
                        dt.Rows[0]["Avatar"] = "~/images/user.png";
                    }
                    FormView2.DataSource = dt;
                    FormView2.DataBind();

                    FormView3.DataSource = dt;
                    FormView3.DataBind();

                string maSanPham = Request.QueryString["idsanpham"].Trim(); 
                DataTable dataTable = SanPhamBUS.getSanPhamOfMaSanPhams(maSanPham);
                DataList1.DataSource = dataTable;
                DataList1.DataBind();

                FormView1.DataSource = dataTable;
                FormView1.DataBind();

                FormViewHoaDon.DataSource = dataTable;
                FormViewHoaDon.DataBind();

                DataListSoLuongMau.DataSource = dataTable;
                DataListSoLuongMau.DataBind();


                pngCongThanhToan.Visible = true;
                pnlGioHang.Visible = false;

                for (int i = 0; i < DataListSoLuongMau.Items.Count; i++)
                {
                    int tien = 0;
                    int soluong = 0;
                    Label lblGiaoHangs = (Label)DataListSoLuongMau.Items[i].FindControl("Label6");
                    Int32.TryParse(lblGiaoHangs.Text.Trim(), out tien);
                    TextBox txtSoLuong = (TextBox)DataListSoLuongMau.Items[i].FindControl("TextBox2");
                    txtSoLuong.Text = "1";
                    Int32.TryParse(txtSoLuong.Text.Trim(), out soluong);

                    tongTienThanhToan += tien * soluong;

                }
                lblTongTiens.Text = tongTienThanhToan.ToString();

                }
                

            }
            else
            {
                Response.Redirect("/Guest/LoginPage.aspx");
            }

          

        }

        #region "CongThanhToan"

        protected void rdVanChuyen_CheckedChanged(object sender, EventArgs e)
        {

            rdBuuDien.Checked = false;
            rdVanChuyen.Checked = true;
            lblGiaoHang.Text = "20,000";


        }

        protected void rdBuuDien_CheckedChanged(object sender, EventArgs e)
        {

            rdBuuDien.Checked = true;
            rdVanChuyen.Checked = false;
            lblGiaoHang.Text = "50,000";


        }

        protected void rdTienMat_CheckedChanged(object sender, EventArgs e)
        {

            rdTienMat.Checked = true;
            rdChuyenKhoan.Checked = false;
            rdTanNoi.Checked = false;
            rdTrucTuyen.Checked = false;

        }

        protected void rdChuyenKhoan_CheckedChanged(object sender, EventArgs e)
        {

            rdChuyenKhoan.Checked = true;
            rdTienMat.Checked = false;
            rdTanNoi.Checked = false;
            rdTrucTuyen.Checked = false;
        }

        protected void rdTanNoi_CheckedChanged(object sender, EventArgs e)
        {

            rdTanNoi.Checked = true;
            rdTienMat.Checked = false;
            rdChuyenKhoan.Checked = false;
            rdTrucTuyen.Checked = false;


        }

        protected void rdTrucTuyen_CheckedChanged(object sender, EventArgs e)
        {

            rdTienMat.Checked = false;
            rdChuyenKhoan.Checked = false;
            rdTanNoi.Checked = false;
            rdTrucTuyen.Checked = true;


        }

        #endregion

        protected void btnTroVe_Click(object sender, EventArgs e)
        {
            string maSanPham = Request.QueryString["idsanpham"].Trim();
            Response.Redirect("/Member/ChiTietSanPham.aspx?pgn=1&id=" + maSanPham);
        }

        protected void btnTiepTuc_Click(object sender, EventArgs e)
        {
            int phuphi = 0;
            if(rdBuuDien.Checked)
            {
                lblpPTGiaoHang.Text = "Bưu Điện";
                phuphi = 50000;
            }
            else
            {
                lblpPTGiaoHang.Text = "Tận Nơi";
                phuphi = 20000;
            }

            if(rdTienMat.Checked)
            {
                lblPTThanhToan.Text = "Tiền Mặt";
            }else if(rdTanNoi.Checked)
            {
                lblPTThanhToan.Text = "Tận Nơi";
            }else if (rdChuyenKhoan.Checked)
            {
                lblPTThanhToan.Text = "Chuyển Khoản";
            }
            else if(rdTrucTuyen.Checked)
            {
                lblPTThanhToan.Text = "Trực Tuyến";
            }

            int soluongsanpham = 0;
            for (int i = 0; i < DataListSoLuongMau.Items.Count; i++)
            {
                TextBox txtSoLuong = (TextBox)DataListSoLuongMau.Items[i].FindControl("TextBox2");
                Int32.TryParse(txtSoLuong.Text.Trim(), out soluongsanpham);

            }
            if (soluongsanpham !=0 )
            {
                pngCongThanhToan.Visible = false;
                pnlGioHang.Visible = true;
                int tongcong = 0;
                for (int i = 0; i < DataList1.Items.Count; i++)
                {
                    Label txtSoLuong = (Label)DataList1.Items[i].FindControl("lblSoLuongSanPham");
                    txtSoLuong.Text = soluongsanpham.ToString();
                    Label lblTongTienThanhToans = (Label)DataList1.Items[i].FindControl("lblTongTienThanhToan");

                    lblTongTienThanhToans.Text = lblTongTiens.Text;
                    Int32.TryParse(lblTongTienThanhToans.Text, out tongcong);

                }
                lblToanBo.Text = (tongcong + phuphi).ToString();
                
            }
           
        }

        protected void btnTroVe2_Click(object sender, EventArgs e)
        {
            pngCongThanhToan.Visible = true;
            pnlGioHang.Visible = false;
        }

        protected void btnHoanTat_Click(object sender, EventArgs e)
        {

        }

        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < DataListSoLuongMau.Items.Count; i++)
            {
                int tien = 0;
                int soluong = 0;
                Label lblGiaoHangs = (Label)DataListSoLuongMau.Items[i].FindControl("Label6");
                Int32.TryParse(lblGiaoHangs.Text.Trim(), out tien);
                TextBox txtSoLuong = (TextBox)DataListSoLuongMau.Items[i].FindControl("TextBox2");
                Int32.TryParse(txtSoLuong.Text.Trim(), out soluong);

                tongTienThanhToan += tien*soluong;
            }
            lblTongTiens.Text = tongTienThanhToan.ToString();
        }
      
    }
}
